package cn.fyl.system.controller;

import cn.fyl.basic.util.JsonResult;
import cn.fyl.basic.util.PageList;
import cn.fyl.system.domain.SystemDetail;
import cn.fyl.system.query.SystemDetailQuery;
import cn.fyl.system.service.ISystemDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/detail")
public class SystemDetailController {

    @Autowired
    private ISystemDetailService systemDetailService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public List<SystemDetail> findAll(){
        return systemDetailService.loadAll();
    }

    /**
     * 查询一个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public SystemDetail findById(@PathVariable("id") Long id){
        return systemDetailService.loadById(id);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            systemDetailService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 添加和修改操作
     * @param SystemDetail
     * @return
     */
    @PostMapping
    public JsonResult save(@RequestBody SystemDetail SystemDetail){
        try {
            if(SystemDetail.getId()==null){
                systemDetailService.save(SystemDetail);
            }else {
                systemDetailService.update(SystemDetail);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }

    }

    /**
     * 分页查询
     * @param
     * @return
     */
    @PutMapping
    public PageList<SystemDetail> queryPage(@RequestBody SystemDetailQuery query){
        try {
            return systemDetailService.queryData(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SystemDetail>();
        }
    }

    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            systemDetailService.patchDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }


    @GetMapping("/loadByTypeId/{id}")
    public List<SystemDetail> loadByTypeId(@PathVariable("id") long id){
        return systemDetailService.loadByTypeId(id);
    }
}
